<?php

class m_loket extends CI_Model{
	
	function _list($data){
		extract($data);
		$sql = "SELECT 
					loket_nama,
					loket_kode,
					loket_nomor,
					loket_transfer,
					loket_id
				FROM m_loket
				WHERE 
					loket_active = '1'
		";
		
		if (@$loket_nama != "") $sql .= " AND loket_nama like '%".@$loket_nama."%'";
		if (@$loket_kode != "") $sql .= " AND loket_kode like '%".@$loket_kode."%'";
		if (@$loket_transfer != "") $sql .= " AND loket_transfer like '%".@$loket_transfer."%'";
		
		$query = $this->db->query($sql);
		$records = $query->num_rows();
		
		$sql = "SELECT * FROM (".$sql.") k LIMIT ".(($page - 1) * $rows)." , ".$rows;
		$query = $this->db->query($sql);
		$result = $query->result();
		
		return '{ 
		  "total": "'.ceil($records / $rows).'", 
		  "page": "'.$page.'", 
		  "records": "'.$records .'",
		  "rows" : '.json_encode($result).'
		}';
	}
	
	function _insert($data){
		$this->db->insert('m_loket',$data);
		return 1;
	}
	
	function _update($data,$id){
		$this->db->where('loket_id',$id);
		$this->db->update('m_loket',$data);
		return 1;
	}
	
	function _delete($data,$id){
		$this->db->where('loket_id',$id);
		$this->db->update('m_loket',$data);
		return 1;
	}
	
	/* another */
	
	
    function getAllAntrian($kode){
        $sql = " SELECT 
                    * 
                FROM 
                    m_loket
                LEFT JOIN 
                    t_pengunjung
                ON m_loket.loket_id = t_pengunjung.m_loket_id
                WHERE 
                    DATE(t_pengunjung.pengunjung_waktu) = DATE(now())
                AND
                    m_loket.loket_kode = '$kode' 
                AND 
                    t_pengunjung.pengunjung_status != 4   
			ORDER BY if (  t_pengunjung.pengunjung_status != 1, 0,1 ) ,t_pengunjung.pengunjung_waktu DESC ";
                
        return $this->db->query($sql);
    }
    function update($pengunjung_id,$data){
        $this->db->where('pengunjung_id',$pengunjung_id);
        $this->db->update('t_pengunjung',$data);
    }
    function getLoketTransfer($kode){
        $sql = "SELECT loket_transfer FROM m_loket WHERE loket_kode = '".$kode."' AND loket_transfer IS NOT NULL LIMIT 1";
        return $this->db->query($sql);  
    }
    
    function getLoketKode($id){
        $sql = "SELECT loket_kode FROM m_loket WHERE loket_id = ".$id."";
        return $this->db->query($sql);
    }
    
    function checkLoketKode($id){
        $sql = "SELECT loket_kode FROM m_loket WHERE loket_id = '".$id."'";
        return $this->db->query($sql);
    }
}